package com.study.web.controller.user;

import cn.dev33.satoken.annotation.SaCheckLogin;

import com.study.common.core.controller.AppBaseController;
import com.study.common.core.domain.PageQuery;
import com.study.common.core.domain.R;
import com.study.common.core.domain.entity.SysDictData;
import com.study.common.core.page.TableDataInfo;
import com.study.common.fdfs.ImageUtil;
import com.study.common.utils.AesGcmUtils;
import com.study.system.service.ISysDictDataService;
import com.study.app.domain.SysDictDataApp;
import com.study.app.domain.bo.AppUserFeedbackAppBo;
import com.study.app.domain.bo.AppUserFeedbackBo;
import com.study.app.domain.vo.AppUserFeedbackVo;
import com.study.app.domain.vo.AppUserVo;
import com.study.app.service.IAppUserFeedbackService;
import com.study.app.service.IAppUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.connector.ClientAbortException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * App用户 -- 问题上报反馈
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/problem")
public class AppUserProblemController extends AppBaseController {

    private final IAppUserFeedbackService iAppUserFeedbackService;

    private final IAppUserService iAppUserService;

    private final ISysDictDataService dictDataService;

    private final ImageUtil imageUtil;

    /**
     * 用户问题图片上传，返回链接地址,支持多张上传,结果是图片以，隔开
     *
     * @param files
     * @return
     */
    @SaCheckLogin
    @PostMapping(value = "/getImage", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<?> getImage(@RequestPart("files") MultipartFile[] files) {
        try {
            StringBuilder urlBuilder = new StringBuilder();
            for (MultipartFile file : files) {
                String address = imageUtil.uploadFile(file);
                urlBuilder.append(imageUtil.location(address)).append(",");
            }
            String url = urlBuilder.toString();
            if (url.endsWith(",")) {
                // 移除末尾的逗号
                url = url.substring(0, url.length() - 1);
            }
            R<String> ok = R.ok();
            ok.setData(url);
            ok.setMsg("操作成功");
            ok.setCode(200);
            return ok;
        } catch (Exception e) {
            // 其他异常处理
            return R.fail("处理文件上传异常");
        }
    }


    /**
     * 获取问题类型列表
     *
     * @return
     */
    @SaCheckLogin
    @GetMapping("/numberType")
    public R<List<SysDictDataApp>> getNumberType() {
        SysDictData dictData = new SysDictData();
        dictData.setDictType("feedback_problem_type");
        dictData.setStatus("0");

        PageQuery pageQuery = new PageQuery();
        pageQuery.setPageNum(1);
        pageQuery.setPageSize(10);
        TableDataInfo<SysDictData> dataInfo = dictDataService.selectPageDictDataList(dictData, pageQuery);

        List<SysDictDataApp> dictDataAppList = new ArrayList<SysDictDataApp>();

        for (SysDictData row : dataInfo.getRows()) {
            SysDictDataApp dataApp = new SysDictDataApp();
            dataApp.setDictLabel(row.getDictLabel());
            dataApp.setDictLabelEn(row.getDictLabelEn());
            dataApp.setDictSort(row.getDictSort());
            dataApp.setDictValue(Integer.valueOf(row.getDictValue()));
            dictDataAppList.add(dataApp);
        }

        return R.ok(dictDataAppList);
    }

    /**
     * 用户提交问题反馈信息列表
     *
     * @return
     */
    @SaCheckLogin
    @PostMapping("/submit")
    public R<?> submitFeedback(@RequestBody AppUserFeedbackAppBo bo) {
        AppUserVo userVo = iAppUserService.selectQueryById(getUserId());
        AppUserFeedbackBo feedbackBo = new AppUserFeedbackBo();
        feedbackBo.setUserCode(userVo.getUserCode());
        feedbackBo.setUserPhone(AesGcmUtils.encrypt(bo.getPhone()));
        feedbackBo.setCategory(bo.getCategory());
        feedbackBo.setContent(bo.getContent());
        feedbackBo.setImages(bo.getImages());
        feedbackBo.setStatus(1);
        feedbackBo.setCreateBy(userVo.getUserNike());
        feedbackBo.setCreateTime(new Date());
        feedbackBo.setUpdateBy(null);
        feedbackBo.setUpdateTime(null);

        if (iAppUserFeedbackService.insertByBo(feedbackBo)) {
            return R.ok();
        }
        return R.fail();

    }


    /**
     * 获取当前用户历史反馈问题信息记录
     *
     * @return
     */
    @SaCheckLogin
    @GetMapping("/getInfo")
    public R<List<AppUserFeedbackVo>> getSubmitFeedbackInfo() {
        AppUserVo userVo = iAppUserService.queryById(getUserId());
        List<AppUserFeedbackVo> userQuery = iAppUserFeedbackService.getUserQuery(userVo.getUserCode());

        if (userQuery.isEmpty()) {
            return R.ok(userQuery);
        }
        return R.ok(userQuery);
    }

}
